package cn.birdbaby.plugins.annotator.classannotator.gen;

import cn.birdbaby.plugins.annotator.classannotator.gen.genannotator.AnnotatorProcessor;
import cn.birdbaby.plugins.constants.Constants;
import cn.birdbaby.plugins.common.CommonUtils;
import com.intellij.codeInsight.actions.LayoutCodeOptions;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.progress.ProgressManager;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiAnnotation;
import com.intellij.psi.PsiFile;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import org.jetbrains.annotations.NotNull;

import java.util.*;

/**
 * @author llorando 4/8/24 9:40 am
 */
public class BBAddGenAnnotationsProcessor {
    private DataContext dataContext;
    private Project project;
    private Editor editor;
    private Document document;

    private PsiFile psiFile;
    private List<PsiFile> psiFiles;
    private LayoutCodeOptions options;
    private List<PsiAnnotation> genAnnotations;

    //默认生成全部注解

    public BBAddGenAnnotationsProcessor(Project project, Editor editor, PsiFile psiFile) {
        this.psiFile = psiFile;
        this.project = project;
        this.editor = editor;
        this.genAnnotations = new ArrayList<>();
    }

    public BBAddGenAnnotationsProcessor(Project project, Editor editor, List<PsiFile> files) {
        this.psiFiles = files;
        this.project = project;
        this.editor = editor;
        this.genAnnotations = new ArrayList<>();

    }

    public void processCode() {
        if (!Objects.isNull(psiFile)) {
            processSingleFile();
        } else {
            processFiles();
        }

    }

    public void processSingleFile() {
        //单个删除的话，在回退栈中，是一个个Gen的操作记录，而不是一组
        if(!CommonUtils.shouldAddGen(psiFile)){
            return;
        }
        String groupId=UUID.randomUUID().toString();
        CommonUtils.processWithGroupId(project,groupId,() ->
                new AnnotatorProcessor(psiFile, groupId, Constants.all).addGenAnnotation()
        );
//        PsiDocumentManager.getInstance(project).commitDocument(document);

    }

    public void processFiles() {
        boolean isSuccess = ProgressManager.getInstance().runProcessWithProgressSynchronously(() -> {
            ProgressIndicator indicator = ProgressManager.getInstance().getProgressIndicator();
            return processFilesUnderProgress(indicator);
        }, "bb add gen", true, project);

        //如果当前编辑器中存在打开的文件，更新一下
        if (editor != null) {
            CodeStyleSettingsManager.getInstance(project).notifyCodeStyleSettingsChanged();
        }
    }

    public boolean processFilesUnderProgress(@NotNull ProgressIndicator indicator) {
        indicator.setIndeterminate(false);
        BBProcessingAddGenAnnotationsTask processor = new BBProcessingAddGenAnnotationsTask(indicator, project,editor);
        processor.process();
        return true;
    }


}
